package com.jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * @author Songzzz
 * @Description
 * @Date 2025/2/24 16:39
 */
public class homework{
    public static final String URL = "jdbc:mysql://localhost:3306/new?serverTimezone=GMT";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";

    public static boolean login2(String username, String password) {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String sql = "select * from t_user where username = ? and password = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static void login3() {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);) {
            System.out.println(connection);
            Statement statement = connection.createStatement();
            System.out.println(statement);

            String sql = "select * from student";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {

                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String cellphone = resultSet.getString("cellphone");
                String address = resultSet.getString("address");
                System.out.println("id" + id + "name" + "age" + age + "gender" + gender + "cellphone" + cellphone + "address" + address);

            }
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    private static void login4() {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);) {
            System.out.println(connection);
            Statement statement = connection.createStatement();
            System.out.println(statement);
            String sql="insert into student(name,age,gender,cellphone,address) values('周六',27,'女','13456789680','武汉')";
            int rows=statement.executeUpdate(sql);
            if(rows>0) {
                System.out.println("数据插入成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void login5() {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);) {
            System.out.println(connection);
            Statement statement = connection.createStatement();
            System.out.println(statement);
            String sql="delete from student where id=5";
            int rows=statement.executeUpdate(sql);
            if(rows>0) {
                System.out.println("数据删除成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void login6() {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);) {
            System.out.println(connection);
            Statement statement = connection.createStatement();
            System.out.println(statement);
            String sql="update student set name='裴培' where id=3";
            int rows=statement.executeUpdate(sql);
            if(rows>0) {
                System.out.println("数据更新成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        if (login2(username, password)) {
            System.out.println("登录成功");
            Scanner scanner1 = new Scanner(System.in);
            System.out.println("请选择操作：1）显示所有学生 2）添加学生 3）删除学生 4）更新学生");
            String choose = scanner1.nextLine();
            switch (choose) {
                case "1":
                    login3();
                    break;
                case "2":
                    login4();
                    break;
                case "3":
                    login5();
                    break;
                case "4":
                    login6();
                    break;
            }
        } else {
            System.out.println("登录失败");
        }

    }


}
